json_normalize Function
Syntax
C result = json_normalize(json as C,columns as C)
Arguments
- jsonTextCharacter
Text to select columns from.
- columnsCharacter
Columns is a '+' and ',' separated list on fields to group on.
The '+' delimiter denotes multiple columns on the same level.
The ',' delimiter denotes separate levels of grouping.
You can mix and match '+' and ',' delimiters.
Description
Generates a grouped JSON content from a flat JSON object list, and supplied columns to group on.
Running the following script converts the flat list into groups of state and city, and finally detail level.
jsonin = <<%str% [ {"name" : "john", "state" : "MA", "city" : "boston"}, {"name" : "fred", "state" : "MA", "city" : "boston"}, {"name" : "liz", "state" : "MA", "city" : "springfield"}, {"name" : "jill", "state" : "NY", "city" : "albany"}, {"name" : "sid", "state" : "NY", "city" : "manhattan"}, {"name" : "tammy", "state" : "NY", "city" : "manhattan"}, {"name" : "fran", "state" : "RI", "city" : "providence"}, {"name" : "tess", "state" : "RI", "city" : "providence"} ] %str% ? json_normalize(jsonin,"state,city")
Produces the output below:
[ { "state": "MA", "_list_1": [ { "city": "boston", "_list_2": [ { "name": "john" }, { "name": "fred" } ] }, { "city": "springfield", "_list_2": [ { "name": "liz" } ] } ] }, { "state": "NY", "_list_1": [ { "city": "albany", "_list_2": [ { "name": "jill" } ] }, { "city": "manhattan", "_list_2": [ { "name": "sid" }, { "name": "tammy" } ] } ] }, { "state": "RI", "_list_1": [ { "city": "providence", "_list_2": [ { "name": "fran" }, { "name": "tess" } ] } ] } ]